package com.rxyb.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *交换机类型:direct(发送到绑定的所有队列中)
 *
 * @author lcf
 * @date 2020-05-25-14:55
 */
@Configuration
public class DirectExchangeConfig {

    /**
     * 注入Direct路由策略的Exchange交换机实例
     *
     * @return
     */
    @Bean(name = "myDirectExchange")
    DirectExchange getDirectExchange() {
        // 创建并返回名为My-Direct-Exchange的交换机
        return new DirectExchange("My-Direct-Exchange");
    }


    /**
     * 将Queue绑定到此directExchange,并指定路由键为"routingKey.First"
     *
     * @param myFirstQueue
     * @param myDirectExchange
     * @return
     */
    @Bean
    Binding bindingQueueOneToDirectExchange(@Qualifier("myFirstQueue") Queue myFirstQueue,
                                            @Qualifier("myDirectExchange") DirectExchange myDirectExchange) {
        return BindingBuilder.bind(myFirstQueue).to(myDirectExchange).with("routingKey.First");
    }


    /**
     * 将Queue绑定到此directExchange,并指定路由键为"requestTest"
     *
     * @param myRequestTestQueue
     * @param myDirectExchange
     * @return
     */
    @Bean
    Binding bindingQueueRequestTestToDirectExchange(@Qualifier("myRequestTestQueue") Queue myRequestTestQueue,
                                                    @Qualifier("myDirectExchange") DirectExchange myDirectExchange) {
        return BindingBuilder.bind(myRequestTestQueue).to(myDirectExchange).with("requestTest.aa");
    }
}
